package com.barbeque.mall.service;

import com.barbeque.mall.bean.BasePageInfo;
import com.barbeque.mall.bean.advertise.BO.AdListBo;
import com.barbeque.mall.bean.advertise.MarketAd;
import com.barbeque.mall.bean.advertise.MarketAdExample;
import com.barbeque.mall.bean.param.CommonData;
import com.barbeque.mall.mapper.MarketAdMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author bai
 * @since 2022/07/15 22:26
 */
@Service
public class AdServiceImpl implements AdService {
    @Autowired
    MarketAdMapper marketAdMapper;
    /**
     * 查询广告列表的逻辑
     * @return
     */
    @Override
    public CommonData<MarketAd> queryAdList(AdListBo adListBo) {
        PageHelper.startPage(adListBo.getPage(),adListBo.getLimit());

        // 查到广告信息列表
        List<MarketAd> marketAds = marketAdMapper.selectByExample(null);
        PageInfo<MarketAd> marketAdPageInfo = new PageInfo<>(marketAds);

        return CommonData.data(marketAdPageInfo);
    }

    /**
     * 删除一条广告的逻辑
     */
    @Override
    public void deleteAd(MarketAd marketAd) {
        marketAdMapper.deleteByPrimaryKey(marketAd.getId());
    }

    /**
     * 根据id修改广告信息的逻辑
     * @param
     */
    @Override
    public MarketAd updateAdInfoById(MarketAd marketAd) {

        marketAdMapper.updateByPrimaryKeySelective(marketAd);

        marketAd = marketAdMapper.selectByPrimaryKey(marketAd.getId());

        Date date = new Date();
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = fmt.format(date);

        marketAd.setUpdateTime(dateStr);
        return marketAd;
    }

    /**
     * 插入一条广告信息的逻辑
     * @param
     */
    @Override
    public MarketAd insertAdInfo(MarketAd marketAd) {
        marketAdMapper.insertSelective(marketAd);
        // 根据id将数据查询出来
        marketAd = marketAdMapper.selectByPrimaryKey(marketAd.getId());
        // 封装时间信息
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss ");
        marketAd.setAddTime(sdf.format(date));
        marketAd.setUpdateTime(sdf.format(date));

        return marketAd;

    }

    /**
     * 广告的模糊查询逻辑
     * @param adListBo
     * @return
     */
    @Override
    public CommonData queryAdListByCondition(AdListBo adListBo) {
        PageHelper.startPage(adListBo.getPage(),adListBo.getLimit());

        MarketAdExample marketAdExample = new MarketAdExample();
        MarketAdExample.Criteria criteria = marketAdExample.createCriteria();
        if(adListBo.getName() != null){
            criteria.andNameLike("%" + adListBo.getName() + "%");
        }
        if(adListBo.getContent() != null){
            criteria.andContentLike("%" + adListBo.getContent() + "%");
        }
        List<MarketAd> marketAds = marketAdMapper.selectByExample(marketAdExample);

        PageInfo<MarketAd> marketAdPageInfo = new PageInfo<>(marketAds);

        return CommonData.data(marketAdPageInfo);
    }


}
